 /**
 * 逻辑、功能相关描述:
 *
 * @author yandawei 编写日期：2008-10-9
 */
package com.ce.pms.common.dao.impl;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.springframework.util.StringUtils;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.common.dao.IComLog4ceBizLogInfoDao;
import com.ce.pms.common.model.ComLog4ceBizLogInfoDomain;
import com.ce.pms.common.util.PmsUtilityComparator;
import com.ce.pms.common.vo.ComLog4ceBizLogInfoVO;
import com.sitechasia.webx.core.support.Page;

/**
 * @author gaosong
 *
 */
public class ComLog4ceBizLogInfoImpl extends PmsBaseDaoImpl<ComLog4ceBizLogInfoDomain> implements IComLog4ceBizLogInfoDao {

	public Page queryComLog4ceBizLogInfo(ComLog4ceBizLogInfoVO comLog4ceBizLogInfoVO) {
		// TODO Auto-generated method stub
		Map<String,Object> paramMap = new HashMap<String,Object>();

		StringBuffer strBuf = new StringBuffer("FROM ComLog4ceBizLogInfoDomain where 1=1 ");

		if(StringUtils.hasText(comLog4ceBizLogInfoVO.getOperator())){

			strBuf.append(" and operator like ").append(":operator");
			paramMap.put("operator", "%"+comLog4ceBizLogInfoVO.getOperator()+"%");

		}
		if(StringUtils.hasText(comLog4ceBizLogInfoVO.getOperation())){

			strBuf.append(" and operation = :operation");
			paramMap.put("operation", comLog4ceBizLogInfoVO.getOperation());

		}
		if(StringUtils.hasText(comLog4ceBizLogInfoVO.getBeginTime() ) )
		{
			strBuf.append(" and operatingTime >= :operatingTime");
			paramMap.put("operatingTime", StringToLongDate(comLog4ceBizLogInfoVO.getBeginTime()));


		}
		if(StringUtils.hasText(comLog4ceBizLogInfoVO.getEndTime())){

			strBuf.append(" and operatingTime < :operatingTime1");
			paramMap.put("operatingTime1", StringToLongDate(StringDate(comLog4ceBizLogInfoVO.getEndTime())));
		}
		if(null!=comLog4ceBizLogInfoVO.getSortStr()&& !"".equals(comLog4ceBizLogInfoVO.getSortStr())){

			strBuf.append(" order by ").append(comLog4ceBizLogInfoVO.getSortStr()).append(" desc");
		}else{
			strBuf.append(" order by operatingTime desc");
		}
		return super.pagedNameQueryByPageNo(strBuf.toString(),
				comLog4ceBizLogInfoVO.getPageNo(),
				comLog4ceBizLogInfoVO.getPageSize(), paramMap);
	}


	@SuppressWarnings("unchecked")
	public   List readProperties(){
		Properties p_Module=new Properties();
    	Properties p=new Properties();

    	List<ComLog4ceBizLogInfoVO> log4ceinfoList1 = new ArrayList<ComLog4ceBizLogInfoVO>();
    	List<ComLog4ceBizLogInfoVO> log4ceinfoList2 = new ArrayList<ComLog4ceBizLogInfoVO>();
    	List<ComLog4ceBizLogInfoVO> list = new ArrayList<ComLog4ceBizLogInfoVO>();
    	try {
    		p_Module.load(ComLog4ceBizLogInfoImpl.class.getResourceAsStream("/ComLog4ceBizLogModule.properties"));
			p.load(ComLog4ceBizLogInfoImpl.class.getResourceAsStream("/ComLog4ceBizLogInfo.properties"));
			Set keys = p.keySet();
			Iterator<String> iterator = keys.iterator();
			while(iterator.hasNext()){
				String key = iterator.next();
				ComLog4ceBizLogInfoVO loginfoVO = new ComLog4ceBizLogInfoVO();
				loginfoVO.setOperation(p.getProperty(key));
				loginfoVO.setOrderKey(key);
				log4ceinfoList1.add(loginfoVO);
			}
			Set keys2 = p_Module.keySet();
			Iterator<String> iterator2 = keys2.iterator();
			while(iterator2.hasNext()){
				String key = iterator2.next();
				ComLog4ceBizLogInfoVO loginfoVO = new ComLog4ceBizLogInfoVO();
				loginfoVO.setOperation(p_Module.getProperty(key));
				loginfoVO.setOrderKey(key);
				log4ceinfoList2.add(loginfoVO);
			}
			java.util.Collections.sort(log4ceinfoList2,PmsUtilityComparator.getComparator());
			for (ComLog4ceBizLogInfoVO comVo : log4ceinfoList2) {
				list.add(comVo);
				List<ComLog4ceBizLogInfoVO> tempList = new ArrayList<ComLog4ceBizLogInfoVO>();
				for (ComLog4ceBizLogInfoVO comVo2 : log4ceinfoList1) {
					if (comVo2.getOrderKey().startsWith(comVo.getOrderKey())) {
						tempList.add(comVo2);
					}
				}
				java.util.Collections.sort(tempList,PmsUtilityComparator.getComparator());
				list.addAll(tempList);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

		return list;
	}


	private static   Long StringToLongDate(String date){

		Long result = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			result= sdf.parse(date).getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	private static String StringDate(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date tempDate = null;
		try {
			tempDate = sdf.parse(date);
			tempDate.setDate(tempDate.getDate()+1);

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sdf.format(tempDate);
	}

}
